﻿using System.Collections;
using Com.Enemy.EnemySystem.EnemyAI;
using Script.EnemySystem.EnemyState;
using UnityEngine;

namespace Script.Com.Human.Enemy.EnemySystem.EnemyStateAI
{
    /// <summary>
    /// 卫兵个体AI
    /// </summary>
    public class GuardStateState : EnemyStateBase
    {

        private bool waitChaseAttack;
        private bool chaseAttacking;
        private bool defencing;

        private IEnumerator hurtCoroutine;
        private Task lastTask;
        private Task nowTask;

        protected override void Start()
        {
            base.Start();
            
            
            animTime.AddStateTime(EnemyState.NormalAttack, 6f/12);
            animTime.AddStateTime(EnemyState.ChaseAttack, 4f/11);
            animTime.AddStateTime(EnemyState.Defensing, 11f/11);
            animTime.AddStateTime(EnemyState.Hurt, 3f/6);
            
            Body.Data.StateColdDown[(int)EnemyState.NormalAttack] = new BaseData(4f/8);
            Body.Data.StateColdDown[(int)EnemyState.ChaseAttack] = new BaseData(2);
            Body.Data.StateColdDown[(int)EnemyState.Defensing] = new BaseData(0.2f);

            Body.Data.State = EnemyState.Idle;
        }
        

        protected override void GetIntoState(EnemyState state)
        {
            if(chaseAttacking || (state == EnemyState.NormalAttack && Body.Data.State == EnemyState.Hurt) || Body.Data.State == EnemyState.Defensing)
                return;
            
            if (state == EnemyState.NormalAttack)
            {
                waitChaseAttack = true;
            }

            if(state == Body.Data.State)
                return;
            
            if (Body.Data.State != EnemyState.Idle)
            {
                animator.SetBool(Body.Data.State.ToString(), false);
                // if(state != EnemyState.NormalAttack)
                //     StopCoroutine(coroutine);
            }
            animator.SetBool(state.ToString(), true);
            Body.Data.State = state;

        }
        /// <summary>
        /// 退出状态 （注：如果上一次攻击为普击，那么如果追击冷却完毕将发动追击）
        /// </summary>
        protected override void ExitState()
        {
            base.ExitState();

            if (waitChaseAttack)
            {
                if (Body.CoolDownSystem.HadColdDown(EnemyState.ChaseAttack))
                {
                    GetIntoState(EnemyState.ChaseAttack);
                    chaseAttacking = true;
                    StartCoroutine(EndChasingAttack());
                }
                
                waitChaseAttack = false;
            }

        }

        IEnumerator EndChasingAttack()
        {
            yield return new WaitForSeconds(4f/8);
            if(!chaseAttacking)
                yield break;
            ExitState(EnemyState.ChaseAttack);
            chaseAttacking = false;
        }

        public void InitChaseVar()
        {
            waitChaseAttack = false;
            chaseAttacking = false;
        }

        public override void PlayState(EnemyState state)
        {
            coroutine = PlayStateOnCoroutine(state);
            StartCoroutine(coroutine);
        }

        
        public override void TakeDamage(int playerId, float damage, float knockback)
        {
            // if (lastTask != null && lastTask.Running)
            // {
            //     //防止被stop
            //     if(!lastTask.Locked)
            //         lastTask.Stop();
            //     else
            //         return;
            // }
            //如果task不是手动stop的，就会自动退出状态
            nowTask = new Task(TakeDamageTask(playerId, new BaseData(damage), knockback));
            nowTask.Finished += delegate(bool manual)
            {
                if (!manual)
                {
                    ExitState();
                }
            };
            lastTask = nowTask;
        }
        
        protected override IEnumerator TakeDamageTask(int playerId, BaseData damage, float knockback)
        {
            // 如果处于追击状态，会受到伤害
            if (chaseAttacking)
            {
                Debug.Log("xx");
                hurtCoroutine = Body.HurtReaction(knockback);
                StartCoroutine(hurtCoroutine);
                yield break;
            }
            //未处于追击状态但在等待进入追击状态则撤销等待进入追击状态
            InitChaseVar();
            if(coroutine != null)
                StopCoroutine(coroutine);
            
            EnemyState state = EnemyState.Hurt;
            float time = .1f;
            //默认进入受伤状态
            if (Body.Data.State != EnemyState.Defensing && Body.Data.State != EnemyState.Hurt)
            {
                hurtCoroutine = Body.HurtReaction(knockback);
                nowTask.Locked = true;
            }
            //防御状态冷却完毕，阻止受伤判定（受伤->防御）
            if (Body.CoolDownSystem.HadColdDown(EnemyState.Defensing))
            {
                if (Body.Data.State != EnemyState.Hurt)
                {
                    state = EnemyState.Defensing;
                    time = 1;
                    nowTask.Locked = true;
                    hurtCoroutine = Body.HurtReaction(50, false);
                }
            }

            GetIntoState(state);
            StartCoroutine(hurtCoroutine);
            yield return new WaitForSeconds(time);
        }
        
    }
}
